package com.yyh.BookLendSystem.ui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.yyh.BookLendSystem.biz.BizException;
import com.yyh.BookLendSystem.biz.ReaderBiz;
import com.yyh.BookLendSystem.util.Utils;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Combo;

public class LoginUI {

	protected Shell shell;
	private Text text;
	private Text text_1;
	private Combo combo;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginUI window = new LoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		shell.setSize(418, 300);
		shell.setText("登陆界面");
		
		Label label = new Label(shell, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_LINK_FOREGROUND));
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label.setBounds(56, 84, 74, 27);
		label.setText("用户名:");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(156, 88, 155, 23);
		
		text_1 = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text_1.setBounds(156, 139, 155, 23);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_LINK_FOREGROUND));
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_1.setText("密码:");
		label_1.setBounds(75, 135, 55, 27);
		
		Button button = new Button(shell, SWT.NONE);
		
		button.setBounds(274, 224, 80, 27);
		button.setText("登录");
		Utils.centerWindows(shell);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 16, SWT.NORMAL));
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		lblNewLabel.setBounds(136, 26, 134, 32);
		lblNewLabel.setText("图书管理系统");
		
		combo = new Combo(shell, SWT.NONE);
		combo.setItems(new String[] {"读者", "管理员"});
		combo.setBounds(156, 189, 155, 25);
		combo.select(0);
		
		Label label_1_1 = new Label(shell, SWT.NONE);
		label_1_1.setText("用户:");
		label_1_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_LINK_FOREGROUND));
		label_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
		label_1_1.setBounds(75, 187, 55, 27);
	
		
		////////////////////////////////////////////
		//登录逻辑
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ReaderBiz biz = new ReaderBiz();
				String name = text.getText();
				String pwd = text_1.getText();
				try {
					biz.login(name, pwd);
					int index =combo.getSelectionIndex();
					LoginUI.this.shell.dispose();
					if(index ==0) {
						new ReaderUI().open();
					}else {
						new MainUI().open();
					}
				} catch (BizException e1) {
					e1.printStackTrace();
					MessageBox mb =new MessageBox(shell);
					mb.setText("消息提示");
					mb.setMessage(e1.getMessage());
					mb.open();
				}
			}
		});

	}
}
